package com.yitiao.socket.server;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

@RequiredArgsConstructor
@Data
public class Response {

    private final OutputStream outputStream;

    private final Request request;

    @SneakyThrows
    public void sendStaticResponse(){
        File file = new File("src/main/resources"+request.getUri());
        FileInputStream inputStream = new FileInputStream(file);
        String response = "HTTP/1.1 200 OK \r\n\r\n";
        outputStream.write(response.getBytes());

        byte[] bytes = new byte[1024];
        while (inputStream.read(bytes,0,1024)!=-1){
            outputStream.write(bytes);
        };

    }
}
